<template> </template>

<script>
export default {
methods: {
    lin(){

      // 准备好要提交的参数
      const params = {
        // order_price: this.getSelectAmount,
        order_price: 1,
        consignee_addr: this.addressStr,
        goods: this.getCartList
          .filter(item => item.goods_state)
          .map(item => {
            return {
              goods_id: item.goods_id,
              goods_number: item.goods_number,
              goods_price: item.goods_price
            }
          })
      }


      const res = await uni.$request({
        url: 'my/orders/create',
        method: 'POST',
        data: params
      })


      if (res.meta.status !== 200) return
      const { order_number } = res.message
      // 支付
      // 1、获取支付参数
      const res2 = await uni.$request({
        url: 'my/orders/req_unifiedorder',
        method: 'POST',
        data: {
          order_number
        }
      })


      if (res2.meta.status === 200) {
        // 2、调用 uni.requestPayment 这个api，传递五个参数，就会弹出支付框（微信提供的）
        uni.requestPayment({
          ...res2.message.pay,
          success: async result => {
            // 3、支付成功之后，告知后台，该笔订单支付成功了
            const res3 = await uni.$request({
              url: 'my/orders/chkOrder',
              method: 'POST',
              data: {
                order_number
              }
            })


            if (res3.meta.status === 200) {
              // 提示
              uni.showToast({
                title: res3.message,
                icon: 'success',
                mask: true,
                duration: 1000
              })
            }
          },
          fail: ({ errMsg }) => {
            if (errMsg === 'requestPayment:fail cancel') {
              uni.showToast({
                title: '您取消了支付!',
                icon: 'none',
                mask: true,
                duration: 1000
              })
            } else {
              uni.showToast({
                title: '支付失败，请联系客服!',
                icon: 'none',
                mask: true,
                duration: 1000
              })
            }
          }
        })

    }
},
</script>

<style></style>
